package com.wyj.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;


public class CommunityUtil {

    //生成随机字符串 (激活码)
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    //Md5加密
    public static String md5(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        return DigestUtils.md5DigestAsHex(key.getBytes());
    }

    //处理json字符串
    public static String getJsonString(int code, String msg, Map<String, Object> map) {
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("code", code);
        jsonObject.put("msg", msg);

        if (map != null) {
            for (String s : map.keySet()) {
                if (s != null) {
                    jsonObject.put(s,map.get(s));
                }
            }
        }
        return jsonObject.toString();
    }

    public static String getJsonString(int code,String msg) {

        return getJsonString(code,msg,null);
    }

    public static String getJsonString(int code) {

        return getJsonString(code,null,null);
    }





}
